package com.saunter.controller;

import com.saunter.model.Admin;
import com.saunter.service.user.AdminService;
import com.saunter.utils.CookieUtils;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Api(tags = {"管理员"})
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Resource
    private ExceptionManager exceptionManager;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Map<String, Object> login(HttpServletResponse response, @RequestParam("email") String email, @RequestParam("passwd") String passwd) {
        Map<String, String> res_login = adminService.login(email, passwd);
        if(null != res_login.get("err_code")) {
            throw exceptionManager.create("Lg001");
        }else {
            String error_code = res_login.get("err_code");
            if(null == error_code) {
                String jwt_token = res_login.get("sid");
                if(null != jwt_token) {
                    CookieUtils.addAdminCookie(response,"sid", jwt_token);
                    return Response.success("登录成功");
                }else {
                    throw exceptionManager.create("Se001");
                }
            }else {
                throw exceptionManager.create(error_code);
            }
        }
    }

    @ApiOperation("登出")
    @GetMapping("logout")
    public Map<String, Object> logout(HttpServletResponse response) {
        adminService.removeCookie(response);
        return Response.success("登出成功");
    }

    @ApiOperation("检测是否登录")
    @GetMapping("/isLogin")
    public Map<String, Object> isLogin() {
        return Response.success("已登录");
    }

    @ApiOperation(value = "修改自身信息", notes = "修改密码或邮箱后需要重新登录")
    @PostMapping("/editAdmin")
    public Map<String, Object> editAdmin(HttpServletRequest request, HttpServletResponse response, @ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id,
                                         @RequestParam(value = "admin_name", required = false)String admin_name, @RequestParam(value = "new_passwd", required = false)String new_passwd, @RequestParam(value = "old_passwd", required = false)String old_passwd, @RequestParam(value = "new_second_passwd", required = false)String new_second_passwd, @RequestParam(value = "email", required = false)String email) {
        if(null != new_passwd) {
            if(!adminService.checkPasswd(admin_id, old_passwd)) {
                throw exceptionManager.create("Lg002");
            }
        }
        String res_checkpw = adminService.checkSecondPasswd(admin_id, second_passwd);
        if("1".equals(res_checkpw)) {
            if(1 == adminService.editAdmin(new Admin(admin_id, admin_name, new_passwd, null, null, email, new_second_passwd, null))) {
                if(null != new_passwd || null != email) {
                    adminService.removeCookie(response);
                }
                return Response.success("修改成功");
            }else {
                throw exceptionManager.create("Sql002");
            }
        }else {
            throw exceptionManager.create(res_checkpw);
        }
    }

    @ApiOperation("获取自身信息")
    @GetMapping("/adminInfo")
    public Map<String, Object> getAdminInfo(@ApiParam(hidden = true)@RequestParam("system_append_admin_id")Integer admin_id) {
        Admin admin = adminService.getSelf(admin_id);
        if(null == admin) {
            throw exceptionManager.create("Lg001");
        }
        Map<String, String> user_info = new HashMap<String, String>();
        user_info.put("email", admin.getEmail());
        user_info.put("name", admin.getName());
        return Response.success(user_info);
    }
}
